package com.zhijian.medical.controller;

import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.vo.request.StudyClassSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.StudyClassSearchVo;
import com.zhijian.medical.entity.vo.response.StudyClassVo;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.StudyClassService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 学习班 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2023-12-07 19:45:41
 */
@Slf4j
@Controller
@Api(tags = "学习班")
public class StudyClassController {

    @Autowired
    private StudyClassService service;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/study/class")
    @ApiOperation("新增")
    public boolean insert(@RequestBody StudyClassSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        PersonDto user = CurrentUserHolder.getUser();
        if (Objects.nonNull(user) && user.getWeatherZxpx() == 1) {
            //如果是专项培训人员，不给操作
            AssertUtil.check(true, "禁止操作");
        }
        return service.save(param);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/study/class/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        PersonDto user = CurrentUserHolder.getUser();
        if (Objects.nonNull(user) && user.getWeatherZxpx() == 1) {
            //如果是专项培训人员，不给操作
            AssertUtil.check(true, "禁止操作");
        }
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/study/class/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody StudyClassSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        PersonDto user = CurrentUserHolder.getUser();
        if (Objects.nonNull(user) && user.getWeatherZxpx() == 1) {
            //如果是专项培训人员，不给操作
            AssertUtil.check(true, "禁止操作");
        }
        param.setId(id);
        return service.modify(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/study/class/{id}")
    @ApiOperation("根据id查询")
    public StudyClassVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/study/class/page")
    @ApiOperation("分页查询")
    public Page<StudyClassVo> selectByCondition(@RequestBody(required = false) StudyClassSearchVo param) {
        if (param == null)
            param = StudyClassSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 列表查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/study/class/list")
    @ApiOperation("列表查询")
    public List<StudyClassVo> selectList(@RequestBody(required = false) StudyClassSearchVo param) {
        if (param == null)
            param = StudyClassSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectList(param);
    }

    /**
     * 报名某个学习班
     *
     * @param id
     * @return
     */
    @Response
    @PostMapping("/study/class/enroll/{id}")
    @ApiOperation("报名某个学习班")
    public boolean enroll(@PathVariable("id") Long id) {
        return service.enroll(id);
    }

}